package com.anjho.bean;

import java.util.ArrayList;
import java.util.List;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import javax.faces.event.AjaxBehaviorEvent;
import javax.persistence.NoResultException;

import com.anjho.dao.DAOAddress;
import com.anjho.dao.DAOCity;
import com.anjho.pojo.Address;
import com.anjho.pojo.City;
import com.anjho.pojo.Estate;
import com.anjho.pojo.Neighborhood;

@ManagedBean(name = "addressBean")
@ViewScoped
public class AddressBean extends GenericBean {

	private static final long serialVersionUID = 1197156706786459597L;
	
	private String zip;
	private List<Address> addressList;
	private Address address;
	private Neighborhood neighborhood;
	private City city;
	private Estate estate;
	private long addCode;
	private boolean addressFound;
	
	public AddressBean() {
		super();
		this.clearForm();
	}
	
	public void clearForm() {
		this.zip = "";
		this.addressList = new ArrayList<Address>();
		this.address = new Address();
		this.neighborhood = new Neighborhood();
		this.city = new City();
		this.estate = new Estate();
		this.addressFound = false;
	}
	
	public String getZip() {
		return zip;
	}

	public void setZip(String zip) {
		this.zip = zip;
	}

	public List<Address> getAddressList() {
		return addressList;
	}

	public void setAddressList(List<Address> addressList) {
		this.addressList = addressList;
	}

	public Address getAddress() {
		return address;
	}

	public void setAddress(Address address) {
		this.address = address;
	}

	public Neighborhood getNeighborhood() {
		return neighborhood;
	}

	public void setNeighborhood(Neighborhood neighborhood) {
		this.neighborhood = neighborhood;
	}

	public City getCity() {
		return city;
	}

	public void setCity(City city) {
		this.city = city;
	}

	public Estate getEstate() {
		return estate;
	}

	public void setEstate(Estate estate) {
		this.estate = estate;
	}

	public long getAddCode() {
		return addCode;
	}

	public void setAddCode(long addCode) {
		this.addCode = addCode;
	}

	public boolean isAddressFound() {
		return addressFound;
	}

	public void setAddressFound(boolean addressFound) {
		this.addressFound = addressFound;
	}

	public void loadAddresses(AjaxBehaviorEvent event) {
		DAOAddress daoAddress = null;
		zip = zip.replaceAll("[^0-9]", "");
		try {
			daoAddress = new DAOAddress();
			this.addressList = daoAddress.getAddressListByZip(zip);
			if (this.addressList.isEmpty()) {
				throw new NoResultException();
			}
			this.setNeighborhood(this.addressList.get(0).getNeighborhood());
			this.setCity(this.neighborhood.getCity());
			this.setEstate(this.city.getEstate());
		} catch (NoResultException e) {
			try {
				/* ALGUMAS CIDADES MENORES PODEM POSSUIR UM ÚNICO CEP E POR */
				/* ESSE MOTIVO AS LINHAS ABAIXO FORAM IMPLEMENTADAS. */
				DAOCity daoCity = new DAOCity();
				this.setCity(daoCity.getCityByZip(zip));
				this.setEstate(this.getCity().getEstate());
				this.setNeighborhood(new Neighborhood());
				this.neighborhood.setCity(this.getCity());
				this.setAddress(new Address());
				this.address.setNeighborhood(this.getNeighborhood());
				this.setAddressFound(false);
			} catch (NoResultException ex) {
				this.clearForm();
				this.setAddressFound(false);
			} catch (Exception ex) {
				this.clearForm();
				this.setAddressFound(false);
				ex.printStackTrace();
			}
		} catch (Exception e) {
			this.clearForm();
			this.setAddressFound(false);
			e.printStackTrace();
		}
	}

}
